<?php

class m_bukti_transaksi extends CI_Model
{

    function __construct()
    {
        parent::__construct();
    }

    public function get($where = null, $limit = null, $offset = null)
    {
        if($where != null) $this->db->where($where);
        if($limit != null && $offset != null) $this->db->limit($limit, $offset);
        return $this->db->get("bukti_transaksi");
    }
    

    function get_all($limit=null, $uri=null)
    {
        $result = $this->db->get('bukti_transaksi', $limit, $uri);
        if ($result->num_rows() > 0)
        {
            return $result->result();
        } else
        {
            return array();
        }
    }

    function getAll(){
        $query = "SELECT bk.ID_BUKTI_TRANSAKSI, bk.NOMER_TRANSAKSI, jt.NAMA_JENIS_TRANSAKSI,
            jdt.NAMA_JENIS_DATA, bk.TGL_TRANSAKSI, bk.KETERANGAN
            FROM bukti_transaksi bk, jenis_transaksi jt, jenis_data_transaksi jdt
            where bk.ID_JENIS_TRANSAKSI = jt.ID_JENIS_TRANSAKSI and bk.ID_JENIS_DATA = jdt.ID_JENIS_DATA";
        $result = $this->db->query($query);
        return $result->result();
    }

    function get_one($id)
    {
        $this->db->where('ID_BUKTI_TRANSAKSI', $id);
        $result = $this->db->get('bukti_transaksi');
        if ($result->num_rows() == 1)
        {
            return $result;
        } else
        {
            return array();
        }
    }

    function get_transaksi($id)
    {
        $this->db->where('NOMER_TRANSAKSI', $id);
        $result = $this->db->get('bukti_transaksi');
        return $result;
    }

    function insert_bukti()
    {
        $data = array(
                'ID_JENIS_TRANSAKSI' => $this->input->post('TRANSAKSI', TRUE),
                'ID_JENIS_DATA' => $this->input->post('DATA_TRANSAKSI', TRUE),
                'NOMER_TRANSAKSI' => $this->input->post('BKK_BKB', TRUE),
                'TGL_TRANSAKSI' => $this->input->post('TANGGAL', TRUE),
                'KETERANGAN' => $this->input->post('KETERANGAN', TRUE),
        );
        $result = $this->db->insert('bukti_transaksi', $data);
        $id = $this->db->insert_id();
        return $id;
    }

    function update_viakasir()
    {
        $data = array(
                'ID_JENIS_TRANSAKSI' => $this->input->post('TRANSAKSI', TRUE),
                'ID_JENIS_DATA' => $this->input->post('DATA_TRANSAKSI', TRUE),
                'NOMER_TRANSAKSI' => $this->input->post('BKK_BKB', TRUE),
                'TGL_TRANSAKSI' => $this->input->post('TANGGAL', TRUE),
                'KETERANGAN' => $this->input->post('KETERANGAN', TRUE),
        );
        $this->db->where('NOMER_TRANSAKSI',$this->input->post('BKK_BKB', TRUE));
        $result = $this->db->update('bukti_transaksi', $data);
        return $result;
    }

    function insert_perkiraan(){
        $data = array(
            'ID_PERKIRAAN' => $this->input->post('KODE_PERKIRAAN', TRUE),
            'ID_BUKTI_TRANSAKSI' => $this->input->post('ID_BUKTI', TRUE),
            'JUMLAH' => $this->input->post('JUMLAH', TRUE),
            'KODE' => $this->input->post('KODE', TRUE)
        );

        $this->db->insert('detail_bukti_transaksi', $data);
        return $this->db->insert_id();
    }

    function get_detail_bukti($id_bukti){
            $this->db->select("perkiraan.*");
            $this->db->select("detail_bukti_transaksi.*");
            $this->db->join("perkiraan", "perkiraan.KODE_PERKIRAAN = detail_bukti_transaksi.ID_PERKIRAAN");
            $this->db->where('detail_bukti_transaksi.ID_BUKTI_TRANSAKSI',$id_bukti);
            return $this->db->get("detail_bukti_transaksi");
    }

    function insert()
    {
        $data = array(                
                'ID_JENIS_TRANSAKSI' => $this->input->post('TRANSAKSI', TRUE),
                'ID_FAKTUR' => $this->input->post('FAKTUR', TRUE),
                'ID_JENIS_DATA' => $this->input->post('DATA_TRANSAKSI', TRUE),
                'ID_PINJAMAN' => $this->input->post('ID_PINJAMAN', TRUE),
                'NOMER_TRANSAKSI' => $this->input->post('BKK_BKB', TRUE),
                'TGL_TRANSAKSI' => $this->input->post('TANGGAL', TRUE),
                'KETERANGAN' => $this->input->post('KETERANGAN', TRUE),
        );

        $result = $this->db->insert('bukti_transaksi', $data);

        $id = $this->db->insert_id();
        $debit = array(
            'ID_PERKIRAAN' => $this->input->post('ID_DEBIT', TRUE),
            'ID_BUKTI_TRANSAKSI' => $id,
            'JUMLAH' => $this->input->post('JUMLAH_DEBIT', TRUE),
            'KODE' => "D",
        );

        $kredit = array(
            'ID_PERKIRAAN' => $this->input->post('ID_KREDIT', TRUE),
            'ID_BUKTI_TRANSAKSI' => $id,
            'JUMLAH' => $this->input->post('JUMLAH_DEBIT', TRUE),
            'KODE' => "K",
        );

        $result += $this->db->insert('detail_bukti_transaksi', $debit);
        $result += $this->db->insert('detail_bukti_transaksi', $kredit);
        return $id;
    }

    function insert_viabank()
    {
        // insert BPB
        // get info usaha
        // sum potongan group by info usaha
        // get perkiraan info usaha
        // every info usaha insert perkiraan via bank
        // get perkiraan jasa info usaha
        // every perk jasa info usaha insert to perkiraan via bank

        // 1
        $data = array(                
                'NOMER_TRANSAKSI' => $this->input->post('BPB', TRUE),
                'TGL_TRANSAKSI' => $this->input->post('TGL_BPB', TRUE),
                'ID_JENIS_DATA' => 2,
                'ID_JENIS_TRANSAKSI' => 2,
        );
        $result = $this->db->insert('bukti_transaksi', $data);
        $id = $this->db->insert_id();

        //2
        $info_usaha = $this->db->get("info_usaha")->result();
        foreach($info_usaha as $usaha){
            $id_info = $usaha->KODE_USAHA;
            //3
            $kode_unit = $this->input->post('KODE_UNIT',TRUE);
            $query = "select sum(pj.POTONGAN-pj.MARGIN) as total
                from pinjaman pj, anggota ag
                where ag.ID_ANGGOTA = pj.ID_ANGGOTA and ag.KODE_UNIT = '$kode_unit'
                and pj.STATUS = 1 and pj.POTONGAN > 0 and pj.ID_INFO_USAHA = $id_info
                ";
            $sum_usaha = $this->db->query($query)->row();
            //4
            $perkiraan = $usaha->PERK_PIUTANG;
            //5
            if ($sum_usaha->total!=null){
                //debit
                $debit = array(
                'ID_PERKIRAAN' => $this->input->post('ID_DEBIT', TRUE),
                'ID_BUKTI_TRANSAKSI' => $id,
                'JUMLAH' => $sum_usaha->total,
                'KODE' => "D",
                );
                $this->db->insert('detail_bukti_transaksi', $debit);
                //kredit
                $kredit = array(
                    'ID_PERKIRAAN' => $perkiraan,
                    'ID_BUKTI_TRANSAKSI' => $id,
                    'JUMLAH' => $sum_usaha->total,
                    'KODE' => "K",
                );
                $result += $this->db->insert('detail_bukti_transaksi', $kredit);
            }
            // end input perkiraan each info usaha

            // 3 (perk jasa)
            $kode_unit = $this->input->post('KODE_UNIT',TRUE);
            $query = "select sum(pj.MARGIN) as total
                from pinjaman pj, anggota ag
                where ag.ID_ANGGOTA = pj.ID_ANGGOTA and ag.KODE_UNIT = '$kode_unit'
                and pj.STATUS = 1 and pj.POTONGAN > 0 and pj.ID_INFO_USAHA = $id_info
                ";
            $sum_usaha = $this->db->query($query)->row();
            //4
            $perkiraan = $usaha->PERK_JASA;
            //5
            if ($sum_usaha->total!=null && $perkiraan!=null){
                //debit
                $debit = array(
                'ID_PERKIRAAN' => $this->input->post('ID_DEBIT', TRUE),
                'ID_BUKTI_TRANSAKSI' => $id,
                'JUMLAH' => $sum_usaha->total,
                'KODE' => "D",
                );
                $this->db->insert('detail_bukti_transaksi', $debit);
                //kredit
                $kredit = array(
                    'ID_PERKIRAAN' => $perkiraan,
                    'ID_BUKTI_TRANSAKSI' => $id,
                    'JUMLAH' => $sum_usaha->total,
                    'KODE' => "K",
                );
                $result += $this->db->insert('detail_bukti_transaksi', $kredit);
            }
        }
        return $id;
    }


    function delete($id)
    {
        $this->db->where('ID_BUKTI_TRANSAKSI', $id);
        $this->db->delete('bukti_transaksi');
    }

    function delete_detail($idbukti){
        $this->db->where("ID_BUKTI_TRANSAKSI", $idbukti);
        return $this->db->delete('detail_bukti_transaksi');
    }

    function delete_detail_perkiraan($id)
    {
        $this->db->where('ID_DETAIL_BUKTI', $id);
        return $this->db->delete('detail_bukti_transaksi');
    }

}
?>
